<template>
<div>
  <p>新闻标题<input type="text" v-model="form.title"></p>
  <p>新闻内容<input type="text" v-model="form.content"></p>
  <button @click="add">添加</button>
  <br>
  <br>
  <table border="1" align="center">
    <tr v-for="i in news">
      <td>{{i.id}}</td>
      <td><button @click="det(i.id)">查看新闻列表</button></td>
    </tr>
  </table>

</div>
</template>

<script>
export default {
  name: "day04_news",
  data(){
    return{
      form:{},
      news:[]

    }
  },
  methods:{
    add(){
      this.$axios.post('users/news/',this.form)
      .then(res=>{
        if (res.data.code==200){
          alert('添加成功')
        }
      })
    },
    det(id){
      let userid = localStorage.getItem('userid')
      if(userid){
        this.$router.push({path:'day04_shoemews',query:{'id':id}})
        console.log(userid)
      }
      else {
        this.$router.push('day04_login')
      }
    },
    get_news(){
      this.$axios.get('users/newss/')
        .then(res=>{
          if(res.data.code==200){
            this.news = res.data.list
          }
        })
    }
  },
  mounted() {
    this.get_news()
  }
}
</script>

<style scoped>

</style>
